import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Cell {
    boolean isTnt=false;
    gamePanel  panel;
    boolean Opened=false;
    boolean Flagged=false;
    int adjacentTnts=0;
    int row, col;
    int mark=0;
    JButton button;
    public Cell(int row, int col, gamePanel panel) {
        this.row = row;
        this.col = col;
        this.panel = panel;
        button = new JButton();
        button.setPreferredSize(new Dimension(50,50));
        button.setFont(new Font("Arial",Font.BOLD,12));
        button.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if(panel.gameOver ||Opened){
                    return;
                }
                if(SwingUtilities.isLeftMouseButton(e)){
                    if(Flagged){
                        return;
                    }
                    panel.openCell(row, col);
                }else if(SwingUtilities.isRightMouseButton(e)){
                    cancelFlag();
                }
            }
        });
    }
    public void open(){
        if (Opened) {
            return;
        }
        Opened = true;
        button.setEnabled(false);
        if(isTnt){
            Color color=new Color(143,6,32);
            button.setText("T");
            button.setBackground(color);
        }else if(adjacentTnts>0){
            button.setText(String.valueOf(adjacentTnts));
        }else{
            button.setBackground(Color.PINK);
        }
    }
    public void cancelFlag(){
        /*
        if(!Opened){
            Flagged=!Flagged;
            button.setText(Flagged ? "F" : "");
            panel.updateTntNum();

        }
         */
        mark=(mark+1)%3;
        switch (mark){
            case 0:
                Flagged=false;
                button.setText("");
                break;
            case 1:
                Flagged=true;
                button.setText("F");
                break;
            case 2:
                Flagged=false;
                button.setText("?");
                break;
        }
        panel.updateTntNum();
    }
}

